﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
//using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyKho.GUI.Library;
using Microsoft.Win32;
using System.Threading;
using QuanLyKho.GUI;
using QuanLyKho.GUI;
using QuanLyKho.GUI.Frm;
using QuanLyKho.Common;
using QuanLyKho.DataAccess;
using QuanLyKho.GUI;

namespace QuanLyKho.GUI
{
    public partial class frmMain : Form
    {
        private System.Windows.Forms.TreeView treHeThong;
        private System.Windows.Forms.TreeView treDanhmuc;
        private System.Windows.Forms.Label lblCreated;
        private System.Windows.Forms.Label lblUserName;
        private System.Windows.Forms.Label lblPosition;
        private System.Windows.Forms.Label lblFullname;
        private System.Windows.Forms.Label lblAccountInfo;
        private System.Windows.Forms.GroupBox gbMenuTT;
        static public System.Windows.Forms.Panel pnLoading;
        static public System.Windows.Forms.Label lblTitile;
        static public System.Windows.Forms.PictureBox pbLoading;
        static public System.Windows.Forms.Timer timerLoading;
        static public Thread threadLoading;
        private string dbName = "";
        public frmMain()
        {
            InitializeComponent();
            //Bottom loading main
            initializeLoading();

        }

        private void initializeLoading()
        {

            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmMain));
            pnLoading = new System.Windows.Forms.Panel();
            lblTitile = new System.Windows.Forms.Label();
            pbLoading = new System.Windows.Forms.PictureBox();
            timerLoading = new System.Windows.Forms.Timer();
            pnLoading.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(pbLoading)).BeginInit();
            // 
            // pnLoading
            // 
            pnLoading.Controls.Add(lblTitile);
            pnLoading.Controls.Add(pbLoading);
            pnLoading.Dock = System.Windows.Forms.DockStyle.Left;
            pnLoading.Location = new System.Drawing.Point(0, 0);
            pnLoading.Name = "pnLoading";
            pnLoading.Size = new System.Drawing.Size(575, 27);
            pnLoading.TabIndex = 3;
            pnLoading.Visible = false;
            // 
            // lblTitile
            // 
            lblTitile.AutoSize = true;
            lblTitile.Location = new System.Drawing.Point(22, 4);
            lblTitile.Name = "lblTitile";
            lblTitile.Size = new System.Drawing.Size(54, 13);
            lblTitile.TabIndex = 3;
            lblTitile.Text = "Đang xử lý dữ liệu...";
            // 
            // pbLoading
            // 
            pbLoading.Image = global::QuanLyKho.GUI.Properties.Resources.ajax_loader;
            pbLoading.Location = new System.Drawing.Point(3, 3);
            pbLoading.Name = "pbLoading";
            pbLoading.Size = new System.Drawing.Size(16, 16);
            pbLoading.SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
            pbLoading.TabIndex = 2;
            pbLoading.TabStop = true;
            //
            // timerLoading
            //
            timerLoading.Enabled = false;
            timerLoading.Interval = 1000;

            pnLoading.ResumeLayout(false);
            pnLoading.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(pbLoading)).EndInit();

            this.panel3.Controls.Add(pnLoading);
        }
        private void subMenu_Exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            string day = string.Empty;
            if (DateTime.Now.DayOfWeek == DayOfWeek.Sunday)
                day = "Chủ nhật";
            else
                day = "thứ " + new clsLibrary().formatDateOfWeek(DateTime.Now.DayOfWeek);
            lblTime.Text = "Hôm nay: " + day + ", ngày " + DateTime.Now.ToString("dd/MM/yyyy") + " - " + DateTime.Now.ToString("H:mm:ss");
        }

        private void subMenu_DangNhapLai_Click(object sender, EventArgs e)
        {

        }

        private void subMenu_ListAccount_Click(object sender, EventArgs e)
        {
            ucListAccount ucLA = new ucListAccount();
            new clsLibrary().processGroupBox(ucLA, gbMain);
        }

        private void btnReLogin_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(new ThreadStart(new clsLibrary().runLogin));
            thread.Name = "Login";
            thread.ApartmentState = ApartmentState.STA;
            thread.Start();
            this.Close();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnChangePassword_Click(object sender, EventArgs e)
        {
            frmChangePassword obj = new frmChangePassword(frmLogin.userID);
            obj.StartPosition = FormStartPosition.CenterParent;
            obj.ShowInTaskbar = false;
            obj.MaximizeBox = false;
            obj.MinimizeBox = false;
            obj.ShowDialog();
        }

        private void danhMụcVậtTưToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ucListMaterial ucLM = new ucListMaterial();
            new clsLibrary().processGroupBox(ucLM, gbMain);
        }

        private void gbMenu_Enter(object sender, EventArgs e)
        {

        }

        private void gbMain_Enter(object sender, EventArgs e)
        {

        }

        private void pnLocation_Paint(object sender, PaintEventArgs e)
        {

        }

        private void HeThong_Click(object sender, EventArgs e)
        {
            //tao treeview
            System.Windows.Forms.TreeNode treListAcount = new System.Windows.Forms.TreeNode("Danh sách tài khoản");
            System.Windows.Forms.TreeNode treConnect = new System.Windows.Forms.TreeNode("Thiết lại kết nối");
            System.Windows.Forms.TreeNode treRestore = new System.Windows.Forms.TreeNode("Khôi phục dữ liệu");
            System.Windows.Forms.TreeNode treBackup = new System.Windows.Forms.TreeNode("Sao lưu dữ liệu");
            System.Windows.Forms.TreeNode treeNodeHT = new System.Windows.Forms.TreeNode("QUẢN TRỊ HỆ THỐNG", new System.Windows.Forms.TreeNode[] {
            treListAcount,
            treConnect,
            treRestore,
            treBackup});
            this.treHeThong = new System.Windows.Forms.TreeView();
            this.SuspendLayout();
            // 
            // treHeThong
            // 
            this.treHeThong.Dock = System.Windows.Forms.DockStyle.Fill;
            this.treHeThong.Location = new System.Drawing.Point(0, 0);
            this.treHeThong.Name = "treHeThong";
            treListAcount.Name = "Node1";
            treListAcount.Text = "Danh sách tài khoản";
            treConnect.Name = "Node2";
            treConnect.Text = "Thiết lại kết nối";
            treRestore.Name = "Node3";
            treRestore.Text = "Khôi phục dữ liệu";
            treBackup.Name = "Node4";
            treBackup.Text = "Sao lưu dữ liệu";
            treeNodeHT.Name = "rdSystem";
            treeNodeHT.Text = "QUẢN TRỊ HỆ THỐNG";
            treeNodeHT.ToolTipText = "menu quản trị hệ thống";
            this.treHeThong.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
            treeNodeHT});
            this.treHeThong.Size = new System.Drawing.Size(225, 353);
            this.treHeThong.TabIndex = 0;
            this.treHeThong.ExpandAll();

            this.treHeThong.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treHeThong_AfterSelect);
            new clsLibrary().processTreview(treHeThong, gbMenu);

        }
        /// <summary>
        /// treHeThong_AfterSelect
        /// Bat su kien khi clich chon treview
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>

        private void treHeThong_AfterSelect(object sender, TreeViewEventArgs e)
        {
            switch (treHeThong.SelectedNode.Name.ToString())
            {
                case "Node1":
                    ucListAccount Account = new ucListAccount();
                    new clsLibrary().processGroupBox(Account, gbMain);
                    break;
                case "Node2":
                    Thread thread = new Thread(new ThreadStart(new clsLibrary().RunConnect));
                    thread.Name = "Connect";
                    thread.ApartmentState = ApartmentState.STA;
                    thread.Start();
                    this.Close();
                    break;
                case "Node3":

                    Thread threadRstore = new Thread(new ThreadStart(new clsLibrary().runRestore));
                    threadRstore.Name = "Restore";
                    threadRstore.ApartmentState = ApartmentState.STA;
                    threadRstore.Start();
                    this.Close();

                    break;
                case "Node4":
                    try
                    {
                        dbName = frmLogin.dbName;
                        string url = new clsLibrary().SaveDialogFile(@"C:\", "", "bak");
                        if (!string.IsNullOrEmpty(url))
                        {
                            clsDbTool objDbTool = new clsDbTool();
                            objDbTool.BackupDatabase(dbName, url);
                            MessageBox.Show("Sao lưu database thành công.");
                        }
                    }
                    catch (Exception ex)
                    {
                        HavitelMessageBox.Show("Có lỗi xảy ra trong quá trình sao lưu dữ liệu.", "Thông báo", HavitelMessageBox.HavitelMessageButton.OK, HavitelMessageBox.HavitelMessageIcon.Error);
                    }

                    break;
            }
        }
        //khi click vao menu danh muc
        private void treDanhmuc_AfterSelect(object sender, TreeViewEventArgs e)
        {
            switch (treDanhmuc.SelectedNode.Name.ToString())
            {
                case "Node1":
                    ucListSupplier ucLS = new ucListSupplier();
                    new clsLibrary().processGroupBox(ucLS, gbMain);
                    break;
                case "Node2":
                    ucListWarehouse ucWarehouse = new ucListWarehouse();
                    new clsLibrary().processGroupBox(ucWarehouse, gbMain);
                    break;
                case "Node3":
                    ucListMaterial ucMaterial = new ucListMaterial();
                    new clsLibrary().processGroupBox(ucMaterial, gbMain);
                    break;
                case "Node4":
                    ucListEquipment ucEquipment = new ucListEquipment();
                    new clsLibrary().processGroupBox(ucEquipment, gbMain);
                    break;
                case "Node5":
                    ucListMachinery ucMachinery = new ucListMachinery();
                    new clsLibrary().processGroupBox(ucMachinery, gbMain);
                    break;
                case "Node6":

                    break;
                case "Node7":
                    uclistProvince ucProvince = new uclistProvince();
                    new clsLibrary().processGroupBox(ucProvince, gbMain);

                    break;
                case "Node8":
                    ucListDistrict ucDistrict = new ucListDistrict();
                    new clsLibrary().processGroupBox(ucDistrict, gbMain);


                    break;
                case "Node9":
                    ucListWard ucListWard = new ucListWard();
                    new clsLibrary().processGroupBox(ucListWard, gbMain);


                    break;
            }

        }
        private void Danhmuc_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.TreeNode treNodeNCC = new System.Windows.Forms.TreeNode("Danh mục nhà cung cấp");
            System.Windows.Forms.TreeNode treNodeKho = new System.Windows.Forms.TreeNode("Danh mục kho");
            System.Windows.Forms.TreeNode treNodeVT = new System.Windows.Forms.TreeNode("Danh mục vật tư");
            System.Windows.Forms.TreeNode treNodeTb = new System.Windows.Forms.TreeNode("Danh mục thiết bị");
            System.Windows.Forms.TreeNode treNodeMay = new System.Windows.Forms.TreeNode("Danh mục máy");
            System.Windows.Forms.TreeNode treNodeTinh = new System.Windows.Forms.TreeNode("Danh mục tỉnh");
            System.Windows.Forms.TreeNode treNodeHuyen = new System.Windows.Forms.TreeNode("Danh mục huyện");
            System.Windows.Forms.TreeNode treNodeXa = new System.Windows.Forms.TreeNode("Danh mục xã");
            System.Windows.Forms.TreeNode treNodeTHX = new System.Windows.Forms.TreeNode("Danh mục tinh-huyện-xã", new System.Windows.Forms.TreeNode[] {
            treNodeTinh,
            treNodeHuyen,
            treNodeXa});
            System.Windows.Forms.TreeNode treeNode10 = new System.Windows.Forms.TreeNode("QUẢN LÝ DANH MỤC", new System.Windows.Forms.TreeNode[] {
            treNodeNCC,
            treNodeKho,
            treNodeVT,
            treNodeTb,
            treNodeMay,
            treNodeTHX});
            this.treDanhmuc = new System.Windows.Forms.TreeView();
            this.SuspendLayout();
            // 
            // treDanhmuc
            // 
            this.treDanhmuc.Dock = System.Windows.Forms.DockStyle.Fill;
            this.treDanhmuc.Location = new System.Drawing.Point(0, 0);
            this.treDanhmuc.Name = "treDanhmuc";
            treNodeNCC.Name = "Node1";
            treNodeNCC.Text = "Danh mục nhà cung cấp";
            treNodeKho.Name = "Node2";
            treNodeKho.Text = "Danh mục kho";
            treNodeVT.Name = "Node3";
            treNodeVT.Text = "Danh mục vật tư";
            treNodeTb.Name = "Node4";
            treNodeTb.Text = "Danh mục thiết bị";
            treNodeMay.Name = "Node5";
            treNodeMay.Text = "Danh mục máy";
            treNodeTinh.Name = "Node7";
            treNodeTinh.Text = "Danh mục tỉnh";
            treNodeHuyen.Name = "Node8";
            treNodeHuyen.Text = "Danh mục huyện";
            treNodeXa.Name = "Node9";
            treNodeXa.Text = "Danh mục xã";
            treNodeTHX.Name = "Node6";
            treNodeTHX.Text = "Danh mục tinh-huyện-xã";
            treeNode10.Name = "Node0";
            treeNode10.Text = "QUẢN LÝ DANH MỤC";
            this.treDanhmuc.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
            treeNode10});
            this.treDanhmuc.Size = new System.Drawing.Size(179, 198);
            this.treDanhmuc.TabIndex = 1;
            this.treDanhmuc.ExpandAll();
            this.treDanhmuc.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treDanhmuc_AfterSelect);
            new clsLibrary().processTreview(treDanhmuc, gbMenu);
        }

        private void mn_Kho_Click(object sender, EventArgs e)
        {
            ucmnuKho ucKH = new ucmnuKho();
            new clsLibrary().processGroupBox(ucKH, gbMenu);
        }
        private void Baocao_Click(object sender, EventArgs e)
        {
            ucmnuBaocao ucBC = new ucmnuBaocao();
            new clsLibrary().processGroupBox(ucBC, gbMenu);
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            this.gbMenuTT = new System.Windows.Forms.GroupBox();
            this.lblCreated = new System.Windows.Forms.Label();
            this.lblUserName = new System.Windows.Forms.Label();
            this.lblPosition = new System.Windows.Forms.Label();
            this.lblFullname = new System.Windows.Forms.Label();
            this.lblAccountInfo = new System.Windows.Forms.Label();
            this.gbMenuTT.SuspendLayout();
            this.SuspendLayout();
            // 
            // gbMenuTT
            // 
            this.gbMenuTT.Controls.Add(this.lblCreated);
            this.gbMenuTT.Controls.Add(this.lblUserName);
            this.gbMenuTT.Controls.Add(this.lblPosition);
            this.gbMenuTT.Controls.Add(this.lblFullname);
            this.gbMenuTT.Controls.Add(this.lblAccountInfo);
            this.gbMenuTT.Dock = System.Windows.Forms.DockStyle.Fill;
            this.gbMenuTT.Location = new System.Drawing.Point(0, 0);
            this.gbMenuTT.Name = "gbMenuTT";
            this.gbMenuTT.Padding = new System.Windows.Forms.Padding(6);
            this.gbMenuTT.Size = new System.Drawing.Size(201, 255);
            this.gbMenuTT.TabIndex = 12;
            this.gbMenuTT.TabStop = false;
            this.gbMenuTT.Text = "Thông tin tài khoản";
            // 
            // lblCreated
            // 
            this.lblCreated.AutoSize = true;
            this.lblCreated.Location = new System.Drawing.Point(7, 95);
            this.lblCreated.Name = "lblCreated";
            this.lblCreated.Size = new System.Drawing.Size(46, 13);
            this.lblCreated.TabIndex = 2;
            this.lblCreated.Text = "Tạo lúc:";
            // 
            // lblUserName
            // 
            this.lblUserName.AutoSize = true;
            this.lblUserName.Location = new System.Drawing.Point(7, 57);
            this.lblUserName.Name = "lblUserName";
            this.lblUserName.Size = new System.Drawing.Size(55, 13);
            this.lblUserName.TabIndex = 2;
            this.lblUserName.Text = "Username";
            // 
            // lblPosition
            // 
            this.lblPosition.AutoSize = true;
            this.lblPosition.Location = new System.Drawing.Point(8, 76);
            this.lblPosition.Name = "lblPosition";
            this.lblPosition.Size = new System.Drawing.Size(44, 13);
            this.lblPosition.TabIndex = 2;
            this.lblPosition.Text = "Position";
            // 
            // lblFullname
            // 
            this.lblFullname.AutoSize = true;
            this.lblFullname.Location = new System.Drawing.Point(8, 38);
            this.lblFullname.Name = "lblFullname";
            this.lblFullname.Size = new System.Drawing.Size(49, 13);
            this.lblFullname.TabIndex = 1;
            this.lblFullname.Text = "Fullname";
            // 
            // lblAccountInfo
            // 
            this.lblAccountInfo.AutoSize = true;
            this.lblAccountInfo.Location = new System.Drawing.Point(8, 19);
            this.lblAccountInfo.Name = "lblAccountInfo";
            this.lblAccountInfo.Size = new System.Drawing.Size(52, 13);
            this.lblAccountInfo.TabIndex = 0;
            this.lblAccountInfo.Text = "Thông tin";
            // 
            // ucmnuThongTin
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Controls.Add(this.gbMenuTT);
            this.Name = "ucmnuThongTin";
            this.Size = new System.Drawing.Size(201, 255);
            this.gbMenuTT.ResumeLayout(false);
            this.gbMenuTT.PerformLayout();
            this.ResumeLayout(false);



            new clsLibrary().process2GroupBox(gbMenuTT, gbMenu);
            if (frmLogin.isAdmin)
                lblAccountInfo.Text = "Xin chào Administrator";
            else
            {
                lblAccountInfo.Text = "Xin chào member";
                //  mn_HeThong.Visible = false;
            }
            if (frmLogin.userID > 0)
            {
                List<clsUser> listUser = new clsDataAccessUser().UserSelectOneWithId(frmLogin.userID);
                if (listUser.Count > 0)
                {
                    lblFullname.Text = "Họ tên: " + listUser[0].Fullname;
                    lblUserName.Text = "Tài khoản: " + listUser[0].UserName;
                    lblPosition.Text = "Chức vụ: " + listUser[0].Position;
                    lblCreated.Text = "Tạo lúc: " + listUser[0].Created.ToString("dd/MM/yyyy H:m:s");
                }
            }
        }



        private void mnChangePass_Click(object sender, EventArgs e)
        {
            frmChangePassword obj = new frmChangePassword(frmLogin.userID);
            obj.StartPosition = FormStartPosition.CenterParent;
            obj.ShowInTaskbar = false;
            obj.MaximizeBox = false;
            obj.MinimizeBox = false;
            obj.ShowDialog();
        }

        private void mnReLogin_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(new ThreadStart(new clsLibrary().runLogin));
            thread.Name = "Login";
            thread.ApartmentState = ApartmentState.STA;
            thread.Start();
            this.Close();
        }

        private void mnExitSys_Click(object sender, EventArgs e)
        {
            this.Close();
        }




    }
}
